﻿// License: The MIT License (MIT) Copyright (c) 2011..2012 Barend Gehrels

// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:

// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.

// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media.Animation;

namespace GrooveSharp
{
    public partial class FullScreenInfo : UserControl, IPlayer
    {
        FlagManager m_flagManager = new FlagManager(0);

        TrackInfo m_info = new TrackInfo();

        public event RoutedEventHandler RatingChanged
        {
            add { AddHandler(Rating.RatingChangedEvent, value); }
            remove { RemoveHandler(Rating.RatingChangedEvent, value); }
        }


        public FullScreenInfo()
        {
            InitializeComponent();
            infoPanel.DataContext = m_info;
        }

        public void Play(TrackInfo info)
        {
            m_flagManager.save();

            m_info = info;

            // Refresh dataContext (currently necessary because these are properties, not Dep.properties)
            infoPanel.DataContext = null;
            infoPanel.DataContext = m_info;

            if (imageAlbum.Source != null)
            {
                //image1.BeginStoryboard(TryFindResource("LoadStoryboard") as Storyboard);
                imageAlbum.BeginStoryboard(TryFindResource("FadeInStoryboard") as Storyboard);
            }
            labelTitle.BeginStoryboard(TryFindResource("ShowStoryboard") as Storyboard);

            m_flagManager = new FlagManager(info.Id);
            checkListBox.ItemsSource = m_flagManager.flags;
        }

        public void Stop()
        {
            infoPanel.DataContext = null;
            checkListBox.ItemsSource = null;

            m_flagManager.save();
        }

        public void SetRating(TrackRating tr)
        {
            m_info.Rating = tr.Value;
        }


        public void FadeOut()
        {
            imageAlbum.BeginStoryboard(TryFindResource("FadeOutStoryboard") as Storyboard);
            labelTitle.BeginStoryboard(TryFindResource("HideStoryboard") as Storyboard);
            m_flagManager.save();
        }

        private void UserControl_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            // Outsmart WPF:

            double ratioX = this.ActualHeight / 1000.0;
            double ratioY = this.ActualWidth / 1000.0;
            double ratio = ratioX < ratioY ? ratioX : ratioY;

            if (ratio < 0.05)
            {
                ratio = 0.05;
            }
            if (ratio > 1.0)
            {
                ratio = 1.0;
            }

            // Keep the image square
            int imageSize = Convert.ToInt32(200 * ratio);
            imageRowDefinition.MinHeight = imageSize;
            imageAlbum.Width = imageSize;
            imageAlbum.Height = imageSize;

            // Adapt all fonts
            labelAlbum.FontSize = 64 * ratio;
            labelArtist.FontSize = 64 * ratio;
            labelTitle.FontSize = 64 * ratio;
            labelYear.FontSize = 32 * ratio;

            // Hide unnecessary controls
            expander1.Visibility = ratio > 0.5 ? Visibility.Visible : Visibility.Collapsed;
        }

        private void CheckBox_Checked(object sender, RoutedEventArgs e)
        {
            m_flagManager.flagChanged = true;
        }

        private void CheckBox_Unchecked(object sender, RoutedEventArgs e)
        {
            m_flagManager.flagChanged = true;
        }

        private void Rating_RatingChanged(object sender, RoutedEventArgs e)
        {
        }
    }
}
